package com.yhgm.utils.timer;

import com.yhgm.constant.Constant;
import org.quartz.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author xinmiao
 * @description
 * @date 2023/4/1 21:32
 */
@Service
public class QuartzUtil {
    @Resource
    private Scheduler scheduler;
    public Boolean add(String id, String corn) {
        if (id==null){
            return false;
        }
        JobBuilder jobBuilder;
        if (id.equals(Constant.BACKUPS_JOB_KEY)){
            jobBuilder = JobBuilder.newJob(BackupsJob.class);
        }else if (id.startsWith(Constant.FILE_DELETE_JOB_KEY)){
            jobBuilder = JobBuilder.newJob(FileDeleteJob.class);
        }else {
            //to cancel
            return false;
            //jobBuilder = JobBuilder.newJob(BackupsJob.class);
        }
        JobDetail jobDetail = jobBuilder
                /*给当前JobDetail添加参数，K V形式*/
                .usingJobData("id", id)
                /*添加认证信息，有3种重写的方法，我这里是其中一种，可以查看源码看其余2种*/
                .withIdentity(id)
                .build();//执行
        CronTrigger trigger= TriggerBuilder.newTrigger()
                .withIdentity(id)
                .withSchedule(CronScheduleBuilder
                        .cronSchedule(corn))
                .build();
        try {
            scheduler.scheduleJob(jobDetail, trigger);
            if (!scheduler.isShutdown()) {
                /*启动*/
                scheduler.start();
            }
        }catch (SchedulerException e){
            return false;
        }
        return true;
    }
    public Boolean update(String id,String corn) {
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(id);
            //获取当前正在运行的触发器
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            if(Objects.isNull(trigger)){
                return false;
            }
            //根据新的执行器重新关联触发器
            trigger = trigger
                    .getTriggerBuilder()
                    .withIdentity(id)
                    .withSchedule(CronScheduleBuilder.cronSchedule(corn))
                    .build();
            //重置对应的job
            scheduler.rescheduleJob(triggerKey, trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public Boolean del(String id) {
        try {
            scheduler.pauseTrigger(TriggerKey.triggerKey(id));//暂停触发器
            scheduler.unscheduleJob(TriggerKey.triggerKey(id));//移除触发器
            scheduler.deleteJob(JobKey.jobKey(id));//删除Job
        }catch (SchedulerException e){
            return false;
        }
        return true;
    }
    public Boolean pause(String id) {
        try {
            scheduler.pauseJob(JobKey.jobKey(id));
        } catch (SchedulerException e) {
            return false;
        }
        return true;
    }
    public Boolean resume(String id) {
        try {
            scheduler.resumeJob(JobKey.jobKey(id));
        } catch (SchedulerException e) {
            return false;
        }
        return true;
    }
}
